package recursos;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JMenuBar;
import org.jdesktop.swingx.JXStatusBar;
import javax.swing.JDesktopPane;

public class MF extends JFrame implements Runnable{

	private static final long serialVersionUID = 1L;
	protected JMenuBar mnMenuBar;
	protected static JDesktopPane dpEscritorio;
	private JXStatusBar stbBarraEstado;
	private JLabel lblUsuario;
	private JLabel lblSistema;
	private JLabel lblFecha;
	private Thread hilo;
	private JLabel lblMemoria;
	private SimpleDateFormat sdf = new SimpleDateFormat(
			"EEEEEEEEE dd 'de' MMMMM 'de' yyyy hh:mm:ss");

	public MF() {
		setTitle(Sesion.titulo);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setIconImage(Sesion.cargarImagen(Sesion.iconRTM));
		setSize(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height-33);
		setVisible(true);
		setLocationRelativeTo(null);
		setResizable(false);

		mnMenuBar = new JMenuBar();
		setJMenuBar(mnMenuBar);

		stbBarraEstado = new JXStatusBar();
		getContentPane().add(stbBarraEstado, BorderLayout.SOUTH);

		dpEscritorio = new JDesktopPane();
		dpEscritorio.setLayout(null);
		dpEscritorio.setBackground(Color.WHITE);
		getContentPane().add(dpEscritorio, BorderLayout.CENTER);

		lblSistema = new JLabel("Sistema: " + Sesion.sistema);
		stbBarraEstado.add(lblSistema);

		JSeparator separador = new JSeparator();
		separador.setOrientation(JSeparator.VERTICAL);
		stbBarraEstado.add(separador);
		
		lblUsuario = new JLabel("Usuario: " + Sesion.usuario);
		stbBarraEstado.add(lblUsuario);

		JSeparator separador3 = new JSeparator();
		separador.setOrientation(JSeparator.VERTICAL);
		stbBarraEstado.add(separador3);

		lblFecha = new JLabel(sdf.format(Calendar.getInstance().getTime()));
		stbBarraEstado.add(lblFecha);

		JSeparator separador4 = new JSeparator();
		separador.setOrientation(JSeparator.VERTICAL);
		stbBarraEstado.add(separador4);
		
		lblMemoria = new JLabel();
		stbBarraEstado.add(lblMemoria);
		
	    hilo=new Thread(this);
	    hilo.start();
	}

	public static boolean isOpen(JInternalFrame iframe){
		JInternalFrame[] iframes = dpEscritorio.getAllFrames();
		for (JInternalFrame jInternalFrame : iframes) {
			if(iframe == jInternalFrame){
				return true;
			}
		}
		return false;
	}
	
	@Override
	public void run() {
		Thread miHilo=Thread.currentThread();
		while(miHilo==hilo) {
	        try{
	            Thread.sleep(1000);
	        }catch (InterruptedException e) { 
	        	e.printStackTrace();
	        }
	        lblFecha.setText(sdf.format(Calendar.getInstance().getTime()));
	        /*
	        lblMemoria.setText("Free Momory:"+(Runtime.getRuntime().freeMemory()/1024)/1024+"\t"+
	        				  " Max Memory:"+(Runtime.getRuntime().maxMemory()/1024)/1024+"\t"+
	        				  " Total Memory:"+(Runtime.getRuntime().totalMemory()/1024)/1024);*/
	        Runtime.getRuntime().gc();
	    }
	}
}
